package vn.vietel.pms.web.action;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;

import vn.vietel.pms.entity.VTeam;
import vn.vietel.pms.service.TeamService;

@Scope(ScopeType.SESSION)
@Name("teamAction")
public class TeamAction {
	@In(required = false)
	@Out(required = false)
	@DataModelSelection("teamItems")
	private VTeam selModel;

	@Logger
	Log log;
	@In("#{teamService}")
	private TeamService teamService;

	@In
	private FacesMessages facesMessages;

	@DataModel
	private List<VTeam> teamItems;

	@DataModel
	private List<SelectItem> allTeams;

	private Long id;

	private boolean editcompleted;

	private boolean editting;

	private String action;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
		editcompleted = false;
		editting = false;
		if (id != null) {
			selModel = teamService.find(id);
			editting = (selModel != null);
		} else {
			selModel = new VTeam();
			editting = false;
		}
	}

	public void createItem() {
		try {
			editcompleted = false;
			teamService.create(selModel);
			getTeamItems();
			editcompleted = true;
			
		} catch (IllegalStateException e) {
			facesMessages.add("Opps!!! Could not insert....");
			facesMessages.add(e.getMessage());
			editcompleted = false;
		}
	}

	public void updateItem() {
		try {
			editcompleted = false;
			teamService.update(selModel);
			getTeamItems();
			editcompleted = true;
			editting = false;

		} catch (IllegalStateException e) {
			facesMessages.add(e.getMessage());
		}
	}

	public void deleteItem(ActionEvent ae) {
		try {
			Long rId = selModel.getId();
			VTeam rgp = teamService.find(rId);
			teamService.delete(rgp);
			getTeamItems();
		} catch (IllegalStateException e) {
			facesMessages.add(e.getMessage());
		}
	}

	@Factory()
	public List<VTeam> getTeamItems() {
		teamItems = teamService.getAll();
		if (teamItems == null)
			teamItems = new LinkedList<VTeam>();
		getAllTeams();
		return teamItems;
	}

	public void setTeamItems(List<VTeam> items) {
		this.teamItems = items;
	}

	public boolean isEditcompleted() {
		return editcompleted;
	}

	public void setEditCompleted(boolean editcompleted) {
		this.editcompleted = editcompleted;
	}

	public boolean isEditting() {
		return editting;
	}

	public void setEditting(boolean editting) {
		this.editting = editting;
	}

	public boolean notEditing() {
		return !editting;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	@Factory()
	public List<SelectItem> getAllTeams() {
		allTeams = new ArrayList<SelectItem>();
		for (VTeam item : teamItems) {
			allTeams.add(new SelectItem(item, item.getTeamName()));
		}
		return allTeams;
	}

	public void setAllTeams(List<SelectItem> allteams) {
		this.allTeams = allteams;
	}

}
